package com.kz.himalaya.fragment.homepage;


import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.test.mock.MockApplication;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.kz.himalaya.BR;
import com.kz.himalaya.R;
import com.kz.himalaya.adapter.CommentAdapter;
import com.kz.himalaya.application.MyApplication;
import com.kz.himalaya.bean.details.DetailListData;
import com.kz.himalaya.bean.details.DetailListEntity;
import com.kz.himalaya.bean.details.TracksList;
import com.kz.himalaya.databinding.FragmentProgramListBinding;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProgramListFragment extends Fragment implements Callback<DetailListEntity> {


    private FragmentProgramListBinding binding;
    private long albumId;
    private CommentAdapter<Object> adapter;

    public ProgramListFragment() {
        // Required empty public constructor
    }

    public static ProgramListFragment newInstance(long albumId) {

        Bundle args = new Bundle();
        args.putLong("albumId",albumId);
        ProgramListFragment fragment = new ProgramListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        albumId = getArguments().getLong("albumId",-1);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_program_list,container,false);
        if (albumId!=-1) {
            ((MyApplication) getActivity().getApplication()).getService().getDetailList(albumId).enqueue(this);
        }
        adapter = new CommentAdapter<>(new ArrayList<>(),getContext(), R.layout.program_list_item, BR.programItem);
        binding.listProgram.setAdapter(adapter);
        return binding.getRoot();
    }

    @Override
    public void onResponse(Call<DetailListEntity> call, Response<DetailListEntity> response) {
        DetailListEntity body = response.body();
        if (body!=null) {
            DetailListData data = body.getData();
            binding.setDetailListData(data);
            adapter.addAll(data.getTracks().getList());
        }
    }

    @Override
    public void onFailure(Call<DetailListEntity> call, Throwable t) {
        t.printStackTrace();
        Log.i("1620", "onFailure: "+t.getMessage());
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
